package websocket

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"net/http"
	"project-im/tools"
)

var (
	upgrade = websocket.Upgrader{
		//允许跨域
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
	//websocket 长连接
	wsConn *websocket.Conn
	err    error
	conn   *tools.Connection
	data   []byte
)

func Start(c *gin.Context) {
	//header中添加Upgrade:websocket   将http升级为ws
	if wsConn, err = upgrade.Upgrade(c.Writer, c.Request, nil); err != nil {
		return
	}
	//初始化ws链接
	if conn, err = tools.NewWebsocket(wsConn); err != nil {
		conn.Close()
	}
}
